<template>
	<view class="content">
		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor='#007AFF'></simple-address>
		<!-- <button class="btns" type="primary" @tap="openAddres">默认打开地址</button> -->
		<view class="box">
			<view class="left">
				收件人
			</view>
			<view class="right">
				<input v-model="name" type="text" placeholder="请输入收件人姓名" />
			</view>
		</view>
		<view class="box">
			<view class="left">
				电话号码
			</view>
			<view class="right">
				<input v-model="tel" type="text" placeholder="请输入收件人电话号码" />
			</view>
		</view>
		<view class="box">
			<view class="left">
				所在地区
			</view>
			<view class="right">
				<input @tap="openAddres" v-model="address" type="text"/>
			</view>
		</view>
		<view class="box">
			<view class="left">
				详细地址
			</view>
			<view class="right">
				<input v-model="detailAddress" type="text" placeholder="输入详细地址" />
			</view>
		</view>
		<view class="box">
			<view class="left">
				设置默认地址
			</view>
			<view class="right">
				<!-- <evan-checkbox v-model="checked"></evan-checkbox> -->
				<!-- <switch color="#354e44" v-model="checked" /> -->
				<evan-switch inactive-color="#FDFDFD"  active-color="#354e44" v-model="checked"></evan-switch>
			</view>
		</view>
		
		<view class="save" @tap="save">
			保存地址
		</view>
		
		

	</view>
</template>

<script>
	import {addAddressInfo} from "@/api/mine/mine.js"
	export default {
		data() {
			return {
				cityPickerValueDefault: [0, 0, 1],
				cityCode:'',
				defaultStatus:0,
				checked:false,
				name:"",
				tel:"",
				address:'请点击选择地址',
				detailAddress:'',
				
				region:'',
				province:'',
				city:''
			}
		},
		methods: {
			openAddres() {
				this.cityPickerValueDefault = [0, 0, 1]
				this.$refs.simpleAddress.open();
			},
			
			onConfirm(e) {
				console.log(e)
				this.address = e.label
				this.province = e.labelArr[0]
				this.city = e.labelArr[1]
				this.region = e.labelArr[2]
				// console.log(this.region)
				this.cityCode = e.provinceCode+'-' +e.cityCode+'-' +e.areaCode
				console.log(this.cityCode)
			},
			save(){
				if(this.checked){
					this.defaultStatus = 1
				}else{
					this.defaultStatus = 0
				}
				console.log(this.defaultStatus)
				addAddressInfo({
					name:this.name,
					phoneNumber:this.tel,
					defaultStatus:this.defaultStatus,
					detailAddress:this.detailAddress,
					cityCode:this.cityCode,
					city:this.city,
					province:this.province,
					region:this.region
				}).then(res=>{
					console.log(res)
					
					if(res.success){
						uni.showToast({
						    title: '操作成功',
						    duration: 2000
						});
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					}else{
						uni.showToast({
						    title: `${res.message}`,
							icon:'none',
						    duration: 2000
						});
					}
					
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 90%;
		box-sizing: border-box;
		height: 150rpx;
		line-height: 50rpx;
		display: flex;
		margin: 0 auto;
		justify-content: space-between;
		align-items: center;
		padding: 40rpx 0rpx;
		border-bottom: 2rpx solid #ccc;

		.left {
			
			
		}
		.input{
			.right{
				margin-right: 50rpx;
			}
		}

		
	}

	.save {
		width: 70%;
		height: 80rpx;
		border-radius: 80rpx;
		background-color: #354e44;
		color: #fff;
		line-height: 80rpx;
		text-align: center;

		margin: 500rpx auto;
	}
	

	
</style>

